package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.CommunityFloorsDto;
import com.ruoyi.system.domain.vo.CommunityFloorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CommunityFloorsMapper;
import com.ruoyi.system.domain.CommunityFloors;
import com.ruoyi.system.service.ICommunityFloorsService;

/**
 * floorsService业务层处理
 * 
 * @author yhb
 * @date 2024-03-26
 */
@Service
public class CommunityFloorsServiceImpl implements ICommunityFloorsService 
{
    @Autowired
    private CommunityFloorsMapper communityFloorsMapper;

    /**
     * 查询floors
     * 
     * @param id floors主键
     * @return floors
     */
    @Override
    public CommunityFloors selectCommunityFloorsById(Long id)
    {
        return communityFloorsMapper.selectCommunityFloorsById(id);
    }

    /**
     * 查询floors列表
     * 
     * @param communityFloorsDto floors
     * @return floors
     */
    @Override
    public List<CommunityFloors> selectCommunityFloorsList(CommunityFloorsDto communityFloorsDto)
    {
        return communityFloorsMapper.selectCommunityFloorsList(communityFloorsDto);
    }

    /**
     * 新增floors
     * 
     * @return 结果
     */
    @Override
    public int insertCommunityFloors(CommunityFloorsDto CommunityFloorsDto) throws Exception {
        CommunityFloorsDto.setCreator(SecurityUtils.getUsername());
        CommunityFloorsDto.setCreateTime(DateUtils.getNowDate());

        CommunityFloorsDto a = getByName(CommunityFloorsDto.getName(), CommunityFloorsDto.getUnitId());
        if(a == null) {
            return communityFloorsMapper.insertCommunityFloors(CommunityFloorsDto);
        } else {
            String name = a.getName();
            Integer buildingId = a.getBuildingId();
            Long unitId = a.getUnitId();
            try{
                if(Objects.equals(CommunityFloorsDto.getBuildingId(), buildingId)) {
                    if (Objects.equals(CommunityFloorsDto.getUnitId(), unitId)) {
                        if (!Objects.equals(CommunityFloorsDto.getName(), name)) {
                            return communityFloorsMapper.insertCommunityFloors(CommunityFloorsDto);
                        } else {
                            throw new Exception();
                        }
                    } else {
                        throw new Exception();
                    }
                } else {
                    throw new Exception();
                }
            } catch (Exception e) {
                throw new Exception(e);
            }
        }

    }

    /**
     * 修改floors
     * 
     * @param communityFloors floors
     * @return 结果
     */
    @Override
    public int updateCommunityFloors(CommunityFloors communityFloors)
    {
        communityFloors.setUpdateTime(DateUtils.getNowDate());
        return communityFloorsMapper.updateCommunityFloors(communityFloors);
    }

    /**
     * 批量删除floors
     * 
     * @param ids 需要删除的floors主键
     * @return 结果
     */
    @Override
    public int deleteCommunityFloorsByIds(Long[] ids)
    {
        return communityFloorsMapper.deleteCommunityFloorsByIds(ids);
    }

    /**
     * 删除floors信息
     * 
     * @param id floors主键
     * @return 结果
     */
    @Override
    public int deleteCommunityFloorsById(Long id)
    {
        return communityFloorsMapper.deleteCommunityFloorsById(id);
    }

    @Override
    public CommunityFloorsDto getByName(String name,Long unitId) {
        return communityFloorsMapper.getByName(name, unitId);
    }

    @Override
    public List<CommunityFloorsVo> listFloors() {
        return communityFloorsMapper.listFloors();
    }
}
